
package com.huaweicloud.haydn.delivercore.agent.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @since 2022-09-24
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum BusinessErrorEnum {

    BASE_NULL_POINTER(HttpStatus.INTERNAL_SERVER_ERROR, "HAYDN.10000001", "Internal error."),
    // 请求参数不合法
    BASE_BAD_REQUEST(HttpStatus.BAD_REQUEST, "HAYDN.10000400", "Invalid request parameters."),
    // 身份认证失败
    BASE_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "HAYDN.10000401", "Authentication failed."),
    // 403=表示禁止访问；客户端没有具备对应的操作权限、或者限制某些IP、未使用SSL等；响应无消息体
    BASE_FORBIDDEN(HttpStatus.FORBIDDEN, "HAYDN.10000403", "Not authorized."),
    // 未找到资源
    BASE_NOT_FOUND(HttpStatus.NOT_FOUND, "HAYDN.10000404", "Resources not found."),
    // 请求过于频繁导致限流
    BASE_REFUSED(HttpStatus.TOO_MANY_REQUESTS, "HAYDN.10000429", "Too many requests."),
    // 重复的提交操作
    BASE_DUPLICATED_POST(HttpStatus.BAD_REQUEST, "HAYDN.10000430", "Repeated submission."),
    // 通用内部错误
    BASE_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "HAYDN.10000500", "Internal error."),
    // 未知的异常
    BASE_UNKNOWN_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "HAYDN.10000501", "Internal error."),

    BASE_ILLEGAL_STATE_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "HAYDN.10000502", "Abnormal status.");

    private HttpStatus httpStatus;

    private String errCode;

    private String errMsg;

    private static final Map<String, BusinessErrorEnum> ERROR_MAP = new HashMap<>();

    static {
        for (BusinessErrorEnum businessErrorEnum : BusinessErrorEnum.values()) {
            ERROR_MAP.put(businessErrorEnum.getErrCode(), businessErrorEnum);
        }
    }

    public static BusinessErrorEnum getErrEnum(String code) {
        return ERROR_MAP.get(code);
    }

    public String getErrMsg() {
        return errMsg;
    }
}
